package com.loveyukari.douyu.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Map;
import java.util.Set;

/**
*
*yangshaohua
*2016年12月8日下午5:48:52
**/
public class DouyuMessageUtil {

	public static byte[] getRequestData(Map<String,Object> param){

		return getByte(getRequestDataStr(param));
	}

	public static String getRequestDataStr(Map<String,Object> param){
		StringBuilder sb = new StringBuilder();
		Set<String> keys = param.keySet();
		for (String key : keys) {
			Object value = param.get(key);
			sb.append(key.replaceAll("/", "@S").replaceAll("@", "@A"));
			sb.append("@=");
			if(value instanceof String){
				sb.append(((String)value).replaceAll("/", "@S").replaceAll("@", "@A"));
			}else if(value instanceof Integer){
				sb.append(value);
			}
			sb.append("/");
		}
		sb.append('\0');
		return sb.toString();
	}

	public static void main(String[] args) {
		String data = "type@=frank/fc@=17828/bnn@=196/list@=uid@AA=33849341@ASnn@AA=缘来我叫小Z@ASic@AA=avanew@AASface@AAS201711@AAS16@AAS22@AAS035941b78cdf583953d2767b1cfa064e@ASfim@AA=67460000@ASbl@AA=28@ASlev@AA=86@ASpg@AA=1@ASrg@AA=4@ASnl@AA=6@ASsahf@AA=0@AS@Suid@AA=100619363@ASnn@AA=神秘人@ASic@AA=avanew@AASface@AAS201701@AAS16@AAS17@AAS6ffeec5803dac53a54ebc24af8ca10c1@ASfim@AA=55768800@ASbl@AA=27@AShd@AA=1@ASri@AA=298aef5e49c4760de8fc54cb385df874c11f1ce12b1cbf6e49efded3f369c8dc08e4fd4548dd7313d67ca84ead4b0651710afb8b6284b99a6386e6bdf11040e02e8557be966384b1a93b18ed41c146aafaf11c2aba41ff1c97123ba49e8637f1cd9f8adb4635cfefcb702ca4882994ec9f387c19cbb2a4e4a7d06fc9e16d986bcfddc8eae3b2a172382ab11516c65a96f688895542f5c3de2b7d1821c91acd6cc04b07d6b3afec86@ASlev@AA=0@ASpg@AA=0@ASrg@AA=0@ASnl@AA=0@ASsahf@AA=0@AS@Suid@AA=2162801@ASnn@AA=小缘的白色相簿@ASic@AA=avanew@AASface@AAS201711@AAS16@AAS16@AASc0d243ccc2ebd05277e22e9268b2d02a@ASfim@AA=52910600@ASbl@AA=27@ASlev@AA=73@ASpg@AA=1@ASrg@AA=4@ASnl@AA=5@ASsahf@AA=0@AS@Suid@AA=3298253@ASnn@AA=Akkyon@ASic@AA=avanew@AASface@AAS201709@AAS16@AAS23@AAS1fb956b530c944b7750616cfcfc7b42e@ASfim@AA=47944000@ASbl@AA=27@ASlev@AA=71@ASpg@AA=1@ASrg@AA=4@ASnl@AA=7@ASsahf@AA=0@AS@Suid@AA=3401526@ASnn@AA=罗宁丶Rhonin@ASic@AA=avatar@AASface@AAS201606@AAS15@AASc080a2ea6314b75bae49b696478dac8f@ASfim@AA=45586200@ASbl@AA=27@ASlev@AA=55@ASpg@AA=1@ASrg@AA=4@ASnl@AA=4@ASsahf@AA=0@AS@Suid@AA=5098416@ASnn@AA=初始原点@ASic@AA=avanew@AASface@AAS201612@AAS24@AAS21@AASac9f52ae9ede73c99fa5b7dafe074819@ASfim@AA=37428700@ASbl@AA=26@ASlev@AA=68@ASpg@AA=1@ASrg@AA=4@ASsahf@AA=0@AS@Suid@AA=70662986@ASnn@AA=爱尚逗蛙是家网红餐厅@ASic@AA=avanew@AASface@AAS201711@AAS02@AAS22@AAS2c64ba69490be46b65385d6203d3cb46@ASfim@AA=35315900@ASbl@AA=26@ASlev@AA=66@ASpg@AA=1@ASrg@AA=4@ASnl@AA=4@ASsahf@AA=0@AS@Suid@AA=15756691@ASnn@AA=缘首麾下今落幕@ASic@AA=avanew@AASface@AAS201706@AAS19@AAS16@AAS9404b6c3e2dad3d27507c92154941ffe@ASfim@AA=27743400@ASbl@AA=25@ASlev@AA=55@ASpg@AA=1@ASrg@AA=4@ASnl@AA=4@ASsahf@AA=0@AS@Suid@AA=17641763@ASnn@AA=凉城dsfwef@ASic@AA=avanew@AASface@AAS201710@AAS17@AAS15@AASb8c2b67676083e5cfec6f14e7a2eadd4@ASfim@AA=27530100@ASbl@AA=25@ASlev@AA=45@ASpg@AA=1@ASrg@AA=1@ASnl@AA=2@ASsahf@AA=0@AS@Suid@AA=13999323@ASnn@AA=小缘赐名的小V@ASic@AA=avanew@AASface@AAS201712@AAS02@AAS14@AAS111cc6addcf23cb51a0bb710c03f76aa@ASfim@AA=26308200@ASbl@AA=24@ASlev@AA=52@ASpg@AA=1@ASrg@AA=4@ASnl@AA=3@ASsahf@AA=0@AS@S/ver@=283140/rid@=196/";
		JSONObject json = parseDataJsonObject(data);
		System.out.println(json);
		System.out.println(json.optJSONArray("list").getJSONObject(0).getString("ic"));
	}

	public static JSONArray parseDataJsonArray(String data){
		JSONArray jsonArray = new JSONArray();
		String[] buff = data.split("/");
		for(String tmp : buff){
			if (StringUtils.isBlank("tmp")){
				continue;
			}
			tmp = tmp.replace("@S", "/").replace("@A", "@");
			if(StringUtils.contains(tmp,"/")){
				if (StringUtils.contains(tmp,"@=")){
					jsonArray.add(parseDataJsonObject(tmp));
				} else if(StringUtils.contains(tmp,"@A=")){
					jsonArray.add(parseDataJsonArray(tmp));
				} else {
					jsonArray.add(tmp);
				}
			} else {
				jsonArray.add(tmp);
			}
		}
		return jsonArray;
	}

	public static JSONObject parseDataJsonObject(String data){
		JSONObject jsonObject = new JSONObject();

		//对数据字符串进行拆分
		String[] buff = data.split("/");
		//分析协议字段中的key和value值
		for(String tmp : buff){
			if (StringUtils.isBlank("tmp")){
				continue;
			}
			//获取key值
			String key = StringUtils.substringBefore(tmp, "@=");
			//获取对应的value值
			String value = StringUtils.substringAfter(tmp, "@=").replace("@S", "/").replace("@A", "@");
			if(StringUtils.contains(value,"/")){
				if (StringUtils.contains(value,"@=")){
					jsonObject.put(key,parseDataJsonObject(value));
				} else  if(StringUtils.contains(value,"@A=")){
					jsonObject.put(key,parseDataJsonArray(value));
				} else {
					jsonObject.put(key,value);
				}
			} else {
				jsonObject.put(key,value);
			}
		}
		return jsonObject;
	}
	
	

    /**
     * 通用方法，将数据转换为小端整数格式
     * @param data
     * @return
     */
    private static byte[] getByte(String data){
    	 ByteArrayOutputStream boutput = new ByteArrayOutputStream();
         DataOutputStream doutput = new DataOutputStream(boutput);

         try
         {
             boutput.reset();
             doutput.write(FormatTransfer.toLH(data.length() + 8), 0, 4);        // 4 bytes packet length
             doutput.write(FormatTransfer.toLH(data.length() + 8), 0, 4);        // 4 bytes packet length
             doutput.write(FormatTransfer.toLH(689), 0, 2); 					 // 2 bytes message type
             doutput.writeByte(0);                                               // 1 bytes encrypt
             doutput.writeByte(0);                                               // 1 bytes reserve
             doutput.writeBytes(data);
         }
         catch(Exception e)
         {
         	e.printStackTrace();
         }

         return boutput.toByteArray();
    }
	
	
}
